﻿using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using SheepStorage.API.Application.Command.DepartmentCommand;
using SheepStorage.API.Application.Command.ProviderManageCommand;
using SheepStorage.API.Baseclass;
using SheepStorage.Domain.Dto.InputDto;
using SheepStorage.Domain.DTO.InputDto;
using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Service.Interface;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace SheepStorage.API.Controllers
{
    /// <summary>
    /// 部门管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DepartmentController : ControllerBase
    {
        private readonly IMediator _mediator;

        IDepartmentService  service;
        private readonly IMapper _mapper;
        public DepartmentController(IMediator mediator, IDepartmentService service, IMapper mapper)
        {
            _mediator = mediator;
            this.service = service;
            _mapper = mapper;
        }

        /// <summary>
        /// 部门添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> Create([FromForm]DepartmentCreateCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 部门删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> Del(DepartmentDelCommand  command)
        {
            return await _mediator.Send(command);
        }
        
        
        /// <summary>
        /// 部门显示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<PageOutput<Department>>> GetDepartment(DepartmentListCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 部门反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Results<Department>> GetFtDepartment(int departmentId)
        {
            return Results<Department>.DataResult(service.GetFtDepartment(departmentId));
        }
        /// <summary>sss
        /// 部门修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> Update(DepartmentUpdaCommand command)
        {
            return await _mediator.Send(command);
        }

      
        /// <summary>
        /// 数据导出
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportCustomer([FromQuery] DepartSelectCommand command)
        {
            //获取数据列表
            var list = await _mediator.Send(command);

            //获取数据
            var data = list.Data;

            //转换为导出类型

            var exportCustomerDto = _mapper.Map<List<Department>, List<DepartmentInput>>(data);

            // 创建一个内存流对象
            MemoryStream ms = new MemoryStream();

            // 异步将导出的客户数据保存到内存流中
            await ms.SaveAsAsync(exportCustomerDto);

            // 将内存流的读取位置重置到开始位置
            ms.Seek(0, SeekOrigin.Begin);

            // 返回一个文件流结果，指定内容类型和文件下载名
            return new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "客户信息.xlsx"
            };
        }

        /// <summary>
        /// 树形列表--部门
        /// </summary>
        /// <param name="departmentParentId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Results<List<Cascader>>> GetCascader(int departmentParentId)
        {
            var command=new DepartmentMenuCommand(departmentParentId);
            return await _mediator.Send(command);   
        }
    }
}
